#include "../parse.h"
#include "../simulate.h"
#include "../node.h"
#include <sstream>

Circuit *C;
int MC_NUM;
int HeuTry;
vector<int> sim_vals;
double criteria;
int main(int argc, char **argv)
{
        
        int trace_size;
	char *filename;
	cout<<"parsing file............."<<endl;
	if (argc < 3) {
		cerr << "Usage: " << argv[0] << " result_file " <<"trace_size" << endl;
		exit(2);
	}
	else if (argc == 3) {
		filename = argv[1];
                trace_size = atoi(argv[2]);
	}
        else
        {
                cout<<"Wrong, retry!"<<endl;
                exit(1);
        }
	parse(filename);
        
        for(int i=0; i<C->signalSize(); ++i)
                C->getSignal(i)->setIndex(i);
        
	for(int i=0; i<C->gateSize(); ++i)
                C->getGate(i)->setGateIndex(i);
	
        Simulate *sim = new Simulate(C);
        
        string tmp = filename;
	tmp += ".result";
        //char *s = new char[strlen(tmp.c_str())];
        //strcpy(s,tmp.c_str());
        cout<<tmp<<endl;
        MC_NUM = 100;
        ifstream in(tmp.c_str());
        const int MAX_LINE_LENGTH = 65536;
        char line_buffer[MAX_LINE_LENGTH];
        
        for(int i = 0; i < trace_size; i++){
                in.getline(line_buffer,MAX_LINE_LENGTH);
                cout<<line_buffer<<endl;
                sim->clearTraced();
                C->getFlipflop(atoi(line_buffer))->getOutput(0)->setTraced(true);
                sim->addTraced(C->getFlipflop(atoi(line_buffer))->getOutput(0));
        }
        
        in.close();
        cout<<"Final visibility is "<<sim->computVN()<<endl;
        
        return 0;
}
